 /* On{X} Script: Notify when battery is charged to 100% every 5 minutes
  *
  * Developer: Gaurav Nandwani
  *
  * Description:
  *
  * This rule monitors battery updates and notifies when:
  * The battery is at 100% and the last known percentage was less than 100%
  * OR The battery is at 100% and the last notification was 5 ago
  * 
  * The script is configurable where battery% and gap between notifications can be defined.
  * 
  * Usage:
  * 
  * Change following variables as needed:
  * 
  * notifyOnBatteryLevel	- Notifies when the battery % is this value.
  * notifyAfterEveryMins	- Notify again after this this interval (in minutes) and if battery % is notifyOnBatteryLevel
  * notificationSubject		- Short Notification Subject - Shown in bold in notification message
  * notificationContent		- Notification Content - Smaller text below the subject
  */

// Initializing variables

var notifyOnBatteryLevel = 100;
var notifyAfterEveryMins = 5; 

var notificationSubject = "Battery Charged";
var notificationContent = "Battery is charged to " + notifyOnBatteryLevel + "%";

// Finished Initializing Variables

console.log('Started script: Notify when battery is charged to ' + notifyOnBatteryLevel + '%.');

// Subscribe to battery updates
device.battery.on('updated', function (signal) {
    var currentBatteryPercent = signal.data.percentage;
    var now = new Date().getTime();
    
    // Get the last seen percentage, default to the current percentage if have not seen any
    var lastBatteryPercent = device.localStorage.getItem('lastBatteryPercent') || currentBatteryPercent;
    lastBatteryPercent = JSON.parse(lastBatteryPercent);
    
    // Read time when last notified
    var lastNotified = device.localStorage.getItem('lastNotified') || now;
    lastNotified = JSON.parse(lastNotified);
    
    // Get time difference between last notification & now and convert to minutes
    var notificationTimeDiff = parseInt((now - lastNotified) / 1000 / 60, 10);
    
    // Raise battery charged notification when: 
    // The battery is at notifyOnBatteryLevel% and the last known percentage was less than notifyOnBatteryLevel%
    // OR The battery is at notifyOnBatteryLevel% and the last notification was notifyAfterEveryMins ago    
    if ((lastBatteryPercent < notifyOnBatteryLevel && currentBatteryPercent === notifyOnBatteryLevel) || 
    (signal.isCharging && currentBatteryPercent === notifyOnBatteryLevel && notificationTimeDiff >= notifyAfterEveryMins)) {
        // Show the notification 
        var notification = device.notifications.createNotification(notificationSubject);
        notification.content = notificationContent;        
        notification.show();
        // Notified: Save notification time
        device.localStorage.setItem('lastNotified', JSON.stringify(now));
        console.info('Notified: ' + notificationSubject + ' - ' + notificationContent);
    }
    
    // Save the current battery percentage
    device.localStorage.setItem('lastBatteryPercent', JSON.stringify(currentBatteryPercent));    
});

// Script completed
console.log('Completed script: Notify when battery is charged to ' + notifyOnBatteryLevel + '%.');
